﻿@using RooHui.Infrastructure.Entities.Entity;
@using RooHui.Tool;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
    var selPolicy = ViewBag.policy;
}
@section head{
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.validate.min.js")"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.metadata.js")"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/messages_cn.js")"></script>
}
<div id="toptoolbar"></div> 
<div id="policygrid">
</div>
<form action="/" method="post" id="frmPolicy" name="frmPolicy" style="background-color: white; display:none; margin-left:30px;">
    <table cellpadding="0" cellspacing="0" class="l-table-edit">
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;策略名称:
            </td>
            <td align="left" class="l-table-edit-td" colspan="2">
                @{
                    if (selPolicy != null)
                    {
                        <input name="txtPolicyName" type="text" id="txtPolicyName" value="@selPolicy.PolicyName" ltype="text" validate="{required:true,notnull:true,maxlength:10}"  nullText="不能为空且不超过10个字符"/>
                    }
                    else
                    {
                        <input name="txtPolicyName" type="text" id="txtPolicyName" ltype="text" validate="{required:true,notnull:true,maxlength:10}"  nullText="不能为空且不超过10个字符"/>
                    }
                 }
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;结算周期:
            </td>
            <td align="left" class="l-table-edit-td">
            @{
                if (selPolicy != null)
                {
                    <input name="txtDuration" type="text" id="txtDuration" value="@selPolicy.Duration" ligerui="width:100" style="width:100;" validate="{required:true,notnull:true,maxlength:2}"/>
                }
                else
                {
                    <input name="txtDuration" type="text" id="txtDuration" ligerui="width:100" style="width:100;" validate="{required:true,notnull:true,maxlength:2}"/>
                }
             }
            </td>
            <td align="left">
                <select id="selDurationType" name="selDurationType" ligerui="width:65" style="width:65;">
                    @{
                        if (selPolicy != null)
                        {
                            if (selPolicy.DurationType == 0)
                            {
                                <option value="0" selected="selected">天</option>
                                <option value="1">月</option>
                            }
                            else if (selPolicy.DurationType == 1)
                            {
                                <option value="0">天</option>
                                <option value="1" selected="selected">月</option>
                            }
                        }
                        else
                        {
                            <option value="0" selected="selected">天</option>
                            <option value="1">月</option>
                        }
                     }
                </select>
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;计算方式:
            </td>
            <td align="left" class="l-table-edit-td" colspan="2">
                <select id="selSettlementType" name="selSettlementType">
                @{
                    if (selPolicy != null)
                    {
                        if (selPolicy.SettlementType == 0)
                        {
                            <option value="0" selected="selected">按比例</option>
                            <option value="1">固定值</option>
                        }
                        else if (selPolicy.SettlementType == 1)
                        {
                            <option value="0">按比例</option>
                            <option value="1" selected="selected">固定值</option>
                        }
                    }
                    else
                    {
                        <option value="0" selected="selected">按比例</option>
                        <option value="1">固定值</option>
                    }
                }
                </select>
            </td>
           
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;结算方式:
            </td>
            <td align="left" class="l-table-edit-td" colspan="2">
                <select id="selBalanceType" name="selBalanceType">
                 @{
                    if (selPolicy != null)
                    {
                        if (selPolicy.BalanceType == 0)
                        {
                            <option value="0" selected="selected">每期等额本金利息</option>
                            <option value="1">每期结息到期还本</option>
                            <option value="2">到期结本息</option>
                        }
                        else if (selPolicy.BalanceType == 1)
                        {
                            <option value="0">每期等额本金利息</option>
                            <option value="1" selected="selected">每期结息到期还本</option>
                            <option value="2">到期结本息</option>
                        }
                        else if (selPolicy.BalanceType == 2)
                        {
                            <option value="0">每期等额本金利息</option>
                            <option value="1">每期结息到期还本</option>
                            <option value="2" selected="selected">到期结本息</option>
                        }
                        else if (selPolicy.BalanceType == 3)
                        {
                            <option value="3" selected="selected">每期固定收益</option>
                            <option value="4">到期固定收益</option>
                        }
                        else if (selPolicy.BalanceType == 4)
                        {
                            <option value="3">每期固定收益</option>
                            <option value="4" selected="selected">到期固定收益</option>
                        }
                    }
                    else
                    {
                        <script type="text/javascript">
                            if ($("#selSettlementType ").val() == 0)
                            {
                                <option value="0" selected="selected">每期等额本金利息</option>
                                <option value="1">每期结息到期还本</option>
                                <option value="2">到期结本息</option>
                            }
                            else if ($("#selSettlementType ").val() == 1)
                            {
                                <option value="3" selected="selected">每期固定收益</option>
                                <option value="4">到期固定收益</option>
                            }
                        </script>
                    }
                }
                </select>
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star"></span>&nbsp;年化收益:
            </td>
            <td align="left" class="l-table-edit-td" colspan="2">
            @{
                if (selPolicy != null)
                {
                    <input name="txtRate" type="text" id="txtRate" ltype="text" value="@selPolicy.Rate" validate="{required:true,notnull:true,maxlength:5}"  nullText="不能为空且不超过5个字符"/>
                }
                else
                {
                    <input name="txtRate" type="text" id="txtRate" ltype="text" validate="{required:true,notnull:true,maxlength:5}"  nullText="不能为空且不超过5个字符"/>
                }
             }
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star"></span>&nbsp;特殊政策:
            </td>
            <td align="left" class="l-table-edit-td" colspan="2">
                <select id="selIsAttach" name="selIsAttach">
                @{
                    if (selPolicy != null)
                    {
                        if (selPolicy.IsAttach == 0)
                        {
                            <option value="false" selected="selected">否</option>
                            <option value="true">是</option>
                        }
                        else if (selPolicy.IsAttach == 1)
                        {
                            <option value="false">否</option>
                            <option value="true" selected="selected">是</option>
                        }
                    }
                    else
                    {
                        <option value="false" selected="selected">否</option>
                        <option value="true">是</option>
                    }
                }
                </select>
            </td>
        </tr>
        <tr>
            <td align="center" colspan="3">
                <span id="lblmsg" class="l-validate-error">&nbsp;</span>
            </td>
        </tr>
    </table>
</form>
<script type="text/javascript">
    var policygrid;

    $(function () {
        $("#toptoolbar").ligerToolBar({ items: [{ text: '新增', click: grid_policy_add, icon: "add" },
            { text: '修改', click: grid_policy_edit, icon: "modify" },
            { text: '删除', click: grid_policy_delete, icon: "delete" },
            { text: '刷新', click: grid_policy_refresh, icon: "refresh" }
            ]
        });
        policygrid = $("#policygrid").ligerGrid({
            columns: [
                { display: '策略名称', name: 'PolicyName', width: '15%', isSort: false },
                { display: '结算间隔', name: 'DurationType', width: '12%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.DurationType) == 0) {
                            return item.Duration + "天";
                        }
                        else if (parseInt(item.DurationType) == 1) {
                            return item.Duration + "月";
                        }
                    }
                },
                { display: '计算方式', name: 'SettlementType', width: '10%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.SettlementType) == 1) return '按比例';
                        else if (parseInt(item.SettlementType) == 2) return '固定值';
                    }
                },
                { display: '年化收益', name: 'Rate', width: '6%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.SettlementType) == 1) {
                            return item.Rate + "%";
                        }
                        else if (parseInt(item.SettlementType) == 2) {
                            return item.Rate + "元";
                        }
                    }
                },
                { display: '结算方式', name: 'BalanceType', width: '10%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.BalanceType) == 0) return '每期等额本金利息';
                        else if (parseInt(item.BalanceType) == 1) return '每期结息到期还本';
                        else if (parseInt(item.BalanceType) == 2) return '到期结本息';
                    }
                },

                { display: '特殊策略', name: 'IsAttach', width: '8%', isSort: false,
                    render: function (item) {
                        if (item.IsAttach == 0) return '否';
                        else if (item.IsAttach == 1) return '是';
                    }
                },
                { display: '状态', name: 'Status', width: '6%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.Status) == 0) return '禁用';
                        else if (parseInt(item.Status) == 1) return '启用';
                    }
                },
                { display: '创建人', name: 'CreateUser', width: '8%', isSort: false },
                { display: '创建时间', name: 'CreateTime', width: '14%', isSort: false },
                {
                    display: '操作', isAllowHide: false,
                    render: function (row) {
                        var html = '<a href="#" onclick="policyEdit(\'' + row.ID + '\',1)">修改</a>&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\', -1)">删除</a>';
                        if (row.Status == 1) {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\',0)">禁用</a>';
                        }
                        else if (row.Status == 0) {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\',1)">启用</a>';
                        }
                        return html;
                    }
                }
                ],
            width: '100%', height: '100%', rownumbers: true,
            dataAction: 'server',
            url: "/Policy/GetPolicyListByPage",
            pageSizeOptions: [20],
            pageSize: 20
        });

        $.metadata.setType("attr", "validate");
        var v = $("#frmPolicy").validate({
            errorPlacement: function (lable, element) {
                if (element.hasClass("l-textarea")) {
                    element.ligerTip({ content: lable.html(), target: element[0] });
                }
                else if (element.hasClass("l-text-field")) {
                    element.parent().ligerTip({ content: lable.html(), target: element[0] });
                }
                else {
                    lable.appendTo(element.parents("td:first").next("td"));
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
                lable.remove();
            },
            submitHandler: function () {
                var durationType = $("#selDurationType ").val();
                var settlementType = $("#selSettlementType ").val();
                var balancetype = $("#selBalanceType ").val();
                var isAttach = $("#selIsAttach ").val();

                if ($('#txtPolicyName').val() == "" || $('#txtPolicyName').val().length > 10) {
                    $('#txtPolicyName').focus();
                    $("#lblmsg").html('用户名不能为空且不超过10个字符');
                    return;
                }
//                var reg = new RegExp("^[0-9]*$");
//                if (!reg.test($('#txtDuration').val())){
//                    $('#txtDuration').focus();
//                    $("#lblmsg").html('结算周期必须为0-99的数字');
//                    return;
                //                }

//                var reg1 = new RegExp("^[0-9]*$");
//                if (!reg1.test($('#txtRate').val())) {
//                    $('#txtRate').focus();
//                    $("#lblmsg").html('年化收益必须为0-99的数字');
//                    return;
//                }
                $("#lblmsg").html('');
                $.ajax({
                    url: "/Policy/PolicyAdd",
                    data: "policyName=" + $("#txtPolicyName").val() + "&durationType=" + durationType + "&balanceType=" + balancetype
                            + "&rate=" + $("#txtRate").val() + "&isAttach=" + isAttach + "&settlementType=" + settlementType
                            + "&duration=" + $("#txtDuration").val(),
                    type: "post",
                    success: function (result) {
                        if (result == "OK") {
                            $.ligerDialog.success("保存成功。");
                            saveDialog.hide();
                            policyreset();
                            grid_policy_refresh();
                        }
                        else {
                            $.ligerDialog.error("保存失败。");
                        }
                    },
                    error: function () {
                        $.ligerDialog.error("保存失败。");
                    }
                });
            }
        });
        $("#frmPolicy").ligerForm();
    }); 

    function search(page) {
        if (page == 1) {
            policygrid.setOptions(
             {
                 parms:
                [
                    { name: "page", value: 1 }
                ]
             });
        }
        else {
            policygrid.setOptions();
        }
        policygrid.loadData();
    }

    // 添加
    var saveDialog;
    function grid_policy_add() {
        saveDialog = $.ligerDialog.open({
            title: "添加策略",
            id: 'policyDialog',
            name: 'policyDialog',
            slide: false,
            isHidden: false,
            modal: true,
            target: $("#frmPolicy"),
            width: 400, height: 320, isResize: false, allowClose: false,
            buttons: [
                    { text: '保存', onclick: save_policy },
                    {
                        text: '取消', onclick: function (item, dialog) {
                            dialog.hide();
                            policyreset();
                            grid_policy_refresh();
                        }
                    }
                ]
        });
    }

    function save_policy() {
        var form = $('#frmPolicy');
        form.submit();
    }

    function policyreset() {
        $('#txtPolicyName').val("");
        $('#txtDuration').val("");
        $('#txtRate').val("");
        $("#isEdit").val("0");
        $("#lblmsg").html("");
    }

    // 修改
    var editDialog;
    function grid_policy_edit() {
        if (policygrid.getSelecteds().length == 0) {
            $.ligerDialog.warn('选择要修改的记录！', '温馨提示');
            return;
        }
        var policyID = policygrid.getSelected().ID;
        policyGet(policyID);
    }

    // 修改
    function policyEdit()
    {
        //location.href = decodeURIComponent("/Policy/PolicyEdit?policyId=" + policyid);
        editDialog = $.ligerDialog.open({
            title: "修改策略",
            id: 'policyDialog',
            name: 'policyDialog',
            slide: false,
            isHidden: false,
            modal: true,
            target: $("#frmPolicy"),
            width: 400, height: 320, isResize: false, allowClose: false,
            buttons: [
                    { text: '保存', onclick: save_policy },
                    {
                        text: '取消', onclick: function (item, dialog) {
                            dialog.hide();
                            policyreset();
                            grid_policy_refresh();
                        }
                    }
                ]
        });
    }

    function policyGet(policyid) {
        $.ajax({
            url: "/Policy/PolicyEdit",
            data: "policyId=" + policyid,
            type: "get",
            success: function (result) {
                policyEdit();
            },
            error: function () {

            }
        });
    }

    function grid_policy_delete() {
        var policyId = "";

        var selected = policygrid.getSelecteds();
        if (selected.length == 0) {
            $.ligerDialog.warn('至少选择一条记录进行删除！', '温馨提示');
            return;
        }

        for (var i = 0; i < selected.length; i++) {
            policyId += selected[i].ID + ",";
        }
        policyId = policyId.substring(0, policyId.length - 1);
        changestate(policyId, -1);

    }

    // 改变策略状态
    function changestate(policyid, destinatstatus) {
        var deststatus;
        if (destinatstatus == 1) {
            deststatus = "启用";
        }
        else if (destinatstatus == 0) {
            deststatus = "禁用";
        }
        else if (destinatstatus == -1) {
            deststatus = "删除";
        }

        $.ligerDialog.confirm('确定要' + deststatus + '选中的策略吗？', '温馨提示', function (result) {
            if (result) {
                debugger;
                $.ajax({
                    url: '/Policy/PolicyStatusChange',
                    data: "policyId=" + policyid + "&destinationStatus=" + destinatstatus,
                    type: "post",
                    success: function (result) {
                        if (result == "OK") {
                            $.ligerDialog.success(deststatus + "成功！");
                            grid_policy_refresh();
                        } else {
                            $.ligerDialog.error(deststatus + '失败！', '温馨提示');
                        }
                    }
                });
            }
        });
    }

    // 刷新
    function grid_policy_refresh() {
        policygrid.setOptions(
             {
                 parms:
                [
                    { name: "page", value: 1 }
                ]
             });
        policygrid.loadData();
    }
 
         
  
</script>